<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manage:medicinePan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="medicinePanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="false"/>
      <el-table-column label="煎药锅编号" align="center" prop="panNumber" />
      <el-table-column label="煎药锅名称" align="center" prop="panName" />
      <el-table-column label="煎药锅型号" align="center" prop="panType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-printer"
            @click="handlePrint(scope.row)"
          >打印二维码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改煎药锅管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="煎药锅编号" prop="panNumber">
          <el-input v-model="form.panNumber" placeholder="请输入煎药锅编号" />
        </el-form-item>
        <el-form-item label="煎药锅名称" prop="panName">
          <el-input v-model="form.panName" placeholder="请输入煎药锅名称" />
        </el-form-item>
        <el-form-item label="煎药锅型号" prop="panType">
          <el-input v-model="form.panType" placeholder="请输入煎药锅型号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMedicinePan, getMedicinePan, delMedicinePan, addMedicinePan, updateMedicinePan } from "@/api/manage/medicinePan";
import Socket from "@/utils/Socket";
import NMPrintSocket from "@/utils/Print";
import { textPrintData } from "@/utils/printData/Text";
import { barcodePrintData } from "@/utils/printData/Barcode";
import { qrCodePrintData } from "@/utils/printData/QrCode";
import { linePrintData } from "@/utils/printData/Line";
import { graphPrintData } from "@/utils/printData/Graph";
import { imgPrintData } from "@/utils/printData/Img";
import { combinationPrintData } from "@/utils/printData/Combination";
import { batchPrintData } from "@/utils/printData/Batch";

export default {
  name: "MedicinePan",
  props: {
    deptId: {
      type: Number,
      required: true,
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 煎药锅管理表格数据
      medicinePanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        panNumber: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        panName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        panType: [
          { required: true, message: "设备型号不能为空", trigger: "blur" }
        ],
      },



    };
  },
  watch:{
    deptId(val, oldValue){
      console.log("medicinalPan",val, oldValue);
      this.getList()
    }
  },
  created() {

  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 查询煎药锅管理列表 */


    // // 开始打印任务
    // async startPrintJobTest(content) {
    //   if (!this.printSocketOpen) return alert("打印服务未开启");
    //   let contentArr = [];
    //   contentArr.push(content);
    //   this.batchPrintJob(contentArr);
    // },
//批量打印列表数据
//     async batchPrintJob(list) {
//       const printQuantity =
//         this.jsonObj.printerImageProcessingInfo.printQuantity;
//       try {
//         const startRes = await this.nMPrintSocket.startJob(
//           this.density,
//           this.label_type,
//           this.print_mode,
//           list.length * printQuantity
//         );
//         if (startRes.resultAck.errorCode == 0) {
//           // 提交打印任务
//           await this.printTag(list, 0);
//         }
//       } catch (err) {
//         console.error(err);
//       }
//     },

    handlePrint(item){
      let c = {type : 'pan', id : item.id, name: item.panName}
      console.log(this.$parent);
      // this.$parent.print(c);
      this.$emit('print', c);
      console.log('调用父组件方法')

    },
    getList() {
      this.loading = true;
      this.queryParams = {
        deptId: this.deptId,
      }
      listMedicinePan(this.queryParams).then(response => {
        this.medicinePanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        panNumber: null,
        panName: null,
        panType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加煎药锅管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMedicinePan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改煎药锅管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.deptId = this.deptId;
          if (this.form.id != null) {
            this.form.deptId = this.deptId;
            updateMedicinePan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMedicinePan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const number = row.panNumber;
      this.$modal.confirm('是否确认删除煎药锅编号为"' + number + '"的数据项？').then(function() {
        return delMedicinePan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/medicinePan/export', {
        ...this.queryParams
      }, `medicinePan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
